package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.dto.GradeOption;
import com.yuesystem.dto.requestbody.GradeRequestBody;
import com.yuesystem.dto.responsebody.OfficeNumberResponseBody;
import com.yuesystem.entity.ChangeItem1;
import com.yuesystem.entity.FixedItem2;
import com.yuesystem.entity.Office;
import com.yuesystem.entity.OfficeItem;
import com.yuesystem.mapper.OfficeMapper;
import com.yuesystem.service.FixedItem2Service;
import com.yuesystem.service.OfficeItemService;
import com.yuesystem.service.OfficeService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




/**
 * 官职接口实现类.
 */
@Service
@Slf4j
public class OfficeServiceImpl extends ServiceImpl<OfficeMapper, Office> implements OfficeService {
  String[][] grades1 = {
      {"万石", "中二千石", "真二千石", "二千石", "比二千石", "千石", "比千石", "比八百石", "六百石",
          "比六百石", "五百石", "四百石", "比四百石", "三百石", "比三百石", "二百石", "百石", "比百石"},
      {"第一品", "第二品", "第三品", "第四品", "第五品", "第六品", "第七品", "第八品", "第九品"},
      {"第一品", "第二品", "第三品", "第四品", "第五品", "第六品", "第七品", "第八品", "第九品"},
      {"第一品", "第二品", "第三品", "第四品", "第五品", "第六品", "第七品", "第八品", "第九品"},
      {"十八班", "十七班", "十六班", "十五班", "十四班", "十三班", "十二班", "十一班", "十班", "九班",
          "八班", "七班", "六班", "五班", "四班", "三班", "二班", "一班"},
      {"第一品", "第二品", "第三品", "第四品", "第五品", "第六品", "第七品", "第八品", "第九品"},
      {"第一品", "从一品", "第二品", "从二品", "第三品", "从三品", "第四品", "从四品", "第五品",
          "从五品", "第六品", "从六品", "第七品", "从七品", "第八品", "从八品", "第九品", "从九品"},
      {"第一品", "从一品", "第二品", "从二品", "第三品", "从三品", "第四品", "从四品",
          "第五品", "从五品", "第六品", "从六品", "第七品", "从七品", "第八品", "从八品", "第九品", "从九品"},
      {"正九命", "九命", "正八命", "八命", "正七命", "七命", "正六命",
          "六命", "正五命", "五命", "正四命", "四命", "正三命", "三命", "正二命", "二命", "正一命", "一命"},
      {"正一品", "从一品", "正二品", "从二品", "正三品", "从三品", "正四品", "从四品", "正五品", "从五品", "正六品",
          "从六品", "正七品", "从七品", "正八品", "从八品", "正九品", "从九品", "视正二品", "视从二品",
          "视正三品", "视从三品", "视从四品", "视从五品", "视正六品", "视从六品", "视正七品", "视从七品",
          "视正八品", "视从八品", "视正九品", "视从九品"},
      {"正一品", "从一品", "正二品", "从二品", "正三品", "从三品", "正四品上", "正四品下", "从四品上", "从四品下", "正五品上", "正五品下",
          "从五品上", "从五品下", "正六品上", "正六品下", "从六品上", "从六品下", "正七品上", "正七品下", "从七品上", "从七品下", "正八品上",
          "正八品下", "从八品上", "从八品下", "正九品上", "正九品下", "从九品上", "从九品下", "视正五品", "视从七品", "内官正一品",
          "内官正二品", "内官正三品", "内官正四品", "宫官正五品", "宫官正六品", "宫官正七品", "宫官正八品", "宫官视七品", "太子内官正三品",
          "太子内官正四品", "太子内官正五品", "太子内官从六品", "太子内官正七品", "太子内官从八品", "太子内官正九品", "太子内官流外三品"},
      {"正一品", "从一品", "正二品", "从二品", "正三品", "从三品", "正四品上", "正四品下", "从四品上",
          "从四品下", "正五品", "从五品", "正六品", "从六品", "正七品", "从七品", "正八品", "从八品", "正九品", "从九品"},
      {"正一品", "从一品", "正二品", "从二品", "正三品", "从三品", "正四品", "从四品", "正五品", "从五品",
          "正六品", "从六品", "正七品", "从七品", "正八品", "从八品", "正九品", "从九品"},
      {"正一品", "从一品", "正二品", "从二品", "正三品", "从三品", "正四品", "从四品", "正五品", "从五品",
          "正六品", "从六品", "正七品", "从七品", "正八品", "从八品", "正九品", "从九品"},
      {"正一品", "从一品", "正二品", "从二品", "正三品", "从三品", "正四品", "从四品", "正五品", "从五品",
          "正六品", "从六品", "正七品", "从七品", "正八品", "从八品", "正九品", "从九品"},
      {"正一品", "从一品", "正二品", "从二品", "正三品", "从三品", "正四品", "从四品", "正五品", "从五品",
          "正六品", "从六品", "正七品", "从七品", "正八品", "从八品", "正九品", "从九品"},
      {"正一品", "从一品", "正二品", "从二品", "正三品", "从三品", "正四品", "从四品", "正五品", "从五品",
          "正六品", "从六品", "正七品", "从七品", "正八品", "从八品", "正九品", "从九品"},
  };
  @Autowired
  private OfficeMapper officeMapper;
  @Autowired
  private OfficeItemService officeItemService;
  @Autowired
  private FixedItem2Service fixedItem2Service;
  
  /**
   * 增加官职.
   */
  @Override
  public boolean add(Office office) {
    List<Long> id = this.getByName(office.getOfficeName());
    if (!id.isEmpty()) {
      return false;
    }
    this.save(office);
    
    return true;
  }
  
  /**
   * 根据官职名字查询对应的ID号.
   *
   * @param name 官职名字
   * @return 官职id
   */
  @Override
  public List<Long> getByName(String name) {
    LambdaQueryWrapper<Office> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.like(Office::getOfficeName, name);
    List<Office> officeList = this.list(queryWrapper);
    
    List<Long> list = officeList.stream().map((item) -> {
      return item.getId();
    }).collect(Collectors.toList());
    
    return list;
  }
  
  /**
   * 根据搜索框中的名称进行查询.
   *
   * @param name 搜索词
   * @return 官职条目
   */
  @Override
  public List<Office> search(String name) {
    LambdaQueryWrapper<Office> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.like(Office::getOfficeName, name);
    List<Office> officeList = officeMapper.selectList(queryWrapper);
    return officeList;
  }
  
  
  /**
   * 获得朝代品阶分布.
   *
   * @return 朝代品阶分布
   */
  @Override
  public List<GradeRequestBody> getByGrade() {
    List<GradeRequestBody> gradeBodyList = new ArrayList<>();
    List<String> dynasty = Arrays.asList("汉代", "三国魏", "晋代", "南朝宋", "南朝梁", "南朝陈",
        "北魏", "北齐", "北周", "隋朝", "唐朝", "宋代", "金朝", "元代", "明代", "清代");
    
    for (int i = 0; i < dynasty.size(); i++) {
      List<GradeOption> options = new ArrayList<>();
      String[] grade = grades1[i];
      int j = 0;
      for (String p : grade) {
        GradeOption option = new GradeOption();
        option.setChildren(new ArrayList<>());
        option.setGrade(p);
        options.add(option);
        j++;
      }
      GradeRequestBody gradeBody = new GradeRequestBody();
      gradeBody.setDynasty(dynasty.get(i));
      gradeBody.setOptions(options);
      gradeBodyList.add(gradeBody);
    }
    return gradeBodyList;
  }
  
  /**
   * 获得每个品阶的官职分页结果.
   */
  @Override
  public Page<Office> getPage(int page, int pageSize, String dynastyGrade) {
    LambdaQueryWrapper<FixedItem2> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(FixedItem2::getRelation, "朝代品阶");
    queryWrapper.likeRight(FixedItem2::getObject, dynastyGrade);
    List<FixedItem2> fixedItems1 = fixedItem2Service.list(queryWrapper);
    
    // 获得官职id集合
    List<Long> ids = fixedItems1.stream().map((item) -> {
      Long officeId = item.getOfficeId();
      return officeId;
    }).collect(Collectors.toList());
    
    //自动去重
    List<Long> listNew = new ArrayList<>(new TreeSet<>(ids));
    
    LambdaQueryWrapper<Office> wrapper = new LambdaQueryWrapper<>();
    wrapper.in(Office::getId, listNew);
    Page<Office> pageInfo = new Page<>(page, pageSize);
    this.page(pageInfo, wrapper);
    return pageInfo;
  }
  
  /**
   * 获得每个品阶的官职分页结果.
   */
  @Override
  public Page<Office> getPage1(int page, int pageSize, String dynasty, String grade) {
    LambdaQueryWrapper<FixedItem2> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(FixedItem2::getRelation, "朝代品阶");
    queryWrapper.likeRight(FixedItem2::getObject, dynasty + grade);
    List<FixedItem2> fixedItems1 = fixedItem2Service.list(queryWrapper);
    
    // 获得官职id集合
    List<Long> ids = fixedItems1.stream().map((item) -> {
      Long officeId = item.getOfficeId();
      return officeId;
    }).collect(Collectors.toList());
    
    //自动去重
    List<Long> listNew = new ArrayList<>(new TreeSet<>(ids));
    
    LambdaQueryWrapper<Office> wrapper = new LambdaQueryWrapper<>();
    wrapper.in(Office::getId, listNew);
    Page<Office> pageInfo = new Page<>(page, pageSize);
    this.page(pageInfo, wrapper);
    return pageInfo;
  }
  
  /**
   * 获得各个朝代官职的数量.
   *
   * @return 官职数量
   */
  @Override
  public List<OfficeNumberResponseBody> getNumber() {
    List<OfficeNumberResponseBody> list = new ArrayList<>();
    String[] dynastys = {"汉代", "三国魏", "晋代", "南朝宋", "南朝梁", "南朝陈", "北魏", "北齐",
        "北周", "隋朝", "唐朝", "宋代", "金朝", "元朝", "明代", "清代"};
    
    for (String dynasty : dynastys) {
      OfficeNumberResponseBody body = new OfficeNumberResponseBody();
      //body.setDynasty(dynasty);
      body.setName(dynasty);
      
      LambdaQueryWrapper<FixedItem2> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.likeRight(FixedItem2::getObject, dynasty)
          .eq(FixedItem2::getRelation, "朝代品阶");
      long count = fixedItem2Service.count(queryWrapper);
      //body.setNumber((int) count);
      body.setValue((int) count);
      list.add(body);
    }
    
    return list;
  }
  
  /**
   * 根据朝代获得一个朝代各个品阶官职的数量.
   *
   * @param index 朝代下标
   * @return 官职数
   */
  @Override
  public List<OfficeNumberResponseBody> getGradeNumber(int index) {
    List<String> dynastyList = Arrays.asList("汉代", "三国魏", "晋代", "南朝宋", "南朝梁", "南朝陈", "北魏",
        "北齐", "北周", "隋朝", "唐朝", "宋代", "金朝", "元代", "明代", "清代");
    String dynasty = dynastyList.get(index);
    String[] grades = grades1[index];
    List<OfficeNumberResponseBody> list = new ArrayList<>();
    for (String grade : grades) {
      LambdaQueryWrapper<OfficeItem> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(OfficeItem::getObject, dynasty + grade)
          .eq(OfficeItem::getRelation, "朝代品阶");
      int total = (int) officeItemService.count(queryWrapper);
      OfficeNumberResponseBody gradeItem = new OfficeNumberResponseBody();
      gradeItem.setName(grade);
      gradeItem.setValue(total);
      list.add(gradeItem);
    }
    return list;
    
  }
  
}
